﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class RuleDetailDAL
    {
        private static RuleDetail CreateRuleDetailFromReader(SqlDataReader reader)
        {
            RuleDetail ruleDetail = new RuleDetail
            {
                RuleDetailID = Utility.StringParse(reader[RuleDetailColumns.RuleDetailID.ToString()]),
                RuleID = Utility.StringParse(reader[RuleDetailColumns.RuleID.ToString()]),
                Line = Utility.IntParse(reader[RuleDetailColumns.Line.ToString()]),
            };

            return ruleDetail;
        }

        private static RuleDetail CreateRuleDetailFromReaderWithChannel(SqlDataReader reader)
        {
            RuleDetail ruleDetail = new RuleDetail
            {
                RuleDetailID = Utility.StringParse(reader[RuleDetailColumns.RuleDetailID.ToString()]),
                RuleID = Utility.StringParse(reader[RuleDetailColumns.RuleID.ToString()]),
                Line = Utility.IntParse(reader[RuleDetailColumns.Line.ToString()]),
                MondayChannelName = Utility.StringParse(reader[RuleDetailColumns.MondayChannelName.ToString()]),
                TuesdayChannelName = Utility.StringParse(reader[RuleDetailColumns.TuesdayChannelName.ToString()]),
                WednesdayChannelName = Utility.StringParse(reader[RuleDetailColumns.WednesdayChannelName.ToString()]),
                ThursdayChannelName = Utility.StringParse(reader[RuleDetailColumns.ThursdayChannelName.ToString()]),
                FridayChannelName = Utility.StringParse(reader[RuleDetailColumns.FridayChannelName.ToString()]),
                SaturdayChannelName = Utility.StringParse(reader[RuleDetailColumns.SaturdayChannelName.ToString()]),
                SundayChannelName = Utility.StringParse(reader[RuleDetailColumns.SundayChannelName.ToString()]),
            };

            return ruleDetail;
        }

        public static List<RuleDetail> GetRuleDetailList(string ruleID)
        {
            try
            {
                List<RuleDetail> ruleDetailList = new List<RuleDetail>();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGetList))
                {
                    while (reader.Read())
                    {
                        ruleDetailList.Add(CreateRuleDetailFromReader(reader));
                    }
                }

                foreach (RuleDetail item in ruleDetailList)
                {
                    item.RuleDetailChannelList = RuleDetailChannelDAL.GetRuleDetailChannelList(item.RuleDetailID);
                }

                return ruleDetailList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_GETLIST);
            }
        }

        public static List<RuleDetail> GetRuleDetailListWithChannel(string ruleID)
        {
            try
            {
                List<RuleDetail> ruleDetailList = new List<RuleDetail>();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailListWithChannel))
                {
                    while (reader.Read())
                    {
                        ruleDetailList.Add(CreateRuleDetailFromReaderWithChannel(reader));
                    }
                }

                return ruleDetailList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailChannelListWithChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_GETLIST);
            }
        }

        /// <summary>
        /// Delete but except id
        /// </summary>
        public static bool DeleteRuleDetail(string ruleID, string ruleDetailID)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetailID;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailDelete) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_DELETE);
            }
            catch (SqlException ex)
            {
                Log.LogEx("DeleteRuleDetail", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_DELETE);
            }
        }

        public bool InsertRuleDetail(RuleDetail ruleDetail)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetail.RuleDetailID;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleDetail.RuleID;
                DataAccessHelper.SqlCmd.Parameters.Add("Line", SqlDbType.Int).Value = ruleDetail.Line;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailInsert) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_INSERT);
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertRuleDetail", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_INSERT);
            }
        }

        public bool UpdateRuleDetail(RuleDetail ruleDetail)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetail.RuleDetailID;
                DataAccessHelper.SqlCmd.Parameters.Add("Line", SqlDbType.Int).Value = ruleDetail.Line;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailUpdate) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_UPDATE);
            }
            catch (SqlException ex)
            {
                Log.LogEx("UpdateRuleDetail", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAIL_UPDATE);
            }
        }
    }
}